package com.sharer.last.controller;


import com.sharer.last.annotation.VisitLogger;
import com.sharer.last.base.constant.CommonConstant;
import com.sharer.last.base.model.R;
import com.sharer.last.enums.VisitBehavior;
import com.sharer.last.model.vo.siteSetting.FriendInfoVO;
import com.sharer.last.model.vo.friend.FriendVO;
import com.sharer.last.service.FriendService;
import com.sharer.last.service.SiteSettingService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 友链
 *
 * @ClassName FriendController
 * @Author wangjin
 * @Date 2023/5/5 21:06
 * @Description
 * @Version 1.0
 */
@RestController
@RequestMapping(CommonConstant.BLOG_FRONT_API + "/friend")
public class FriendController {
    @Resource
    FriendService friendService;

    @Resource
    SiteSettingService siteSettingService;

    /**
     * 获取友链页面
     *
     * @return
     */
    @VisitLogger(VisitBehavior.FRIEND)
    @GetMapping("/getFriend")
    public R friends() {
        List<FriendVO>      friendList = friendService.selectAllList();
        FriendInfoVO        friendInfo = siteSettingService.getFriendInfo(true);
        Map<String, Object> map        = new HashMap<>(4);
        map.put("friendList", friendList);
        map.put("friendInfo", friendInfo);
        return R.ok(map);
    }

    /**
     * 按昵称增加友链浏览次数
     *
     * @param nickname 友链昵称
     * @return
     */
    @VisitLogger(VisitBehavior.CLICK_FRIEND)
    @PostMapping("/addViews")
    public R addViews(@RequestBody String nickname) {
        friendService.updateViewsByNickname(nickname);
        return R.handle();
    }
}
